using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class SnsNewMsgHintCtrl : UserControl
	{
		private const string TAG = "SnsNewMsgHintCtrl";
		private static SnsNewMsgHintCtrl sInstance;
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchMsg;
		private object mObjectWatcher;
		internal UserHeadCtrl ctrlImgHead;
		internal TextBlock txtMsgCnt;
		private bool _contentLoaded;
		public static SnsNewMsgHintCtrl GetInstance()
		{
			if (SnsNewMsgHintCtrl.sInstance == null)
			{
				return new SnsNewMsgHintCtrl();
			}
			if (SnsNewMsgHintCtrl.sInstance.get_Parent() != null)
			{
				Panel panel = SnsNewMsgHintCtrl.sInstance.get_Parent() as Panel;
				panel.get_Children().Remove(SnsNewMsgHintCtrl.sInstance);
			}
			return SnsNewMsgHintCtrl.sInstance;
		}
		public SnsNewMsgHintCtrl()
		{
			ShowObjectInMemory.AddOneObjectRecord(this);
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
			this.ctrlImgHead.set_IsEnabled(false);
			SnsNewMsgHintCtrl.sInstance = this;
			this.registerEvent();
			base.set_Visibility(1);
		}
		~SnsNewMsgHintCtrl()
		{
			this.unRegisterEvent();
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchMsg == null)
			{
				this.mWatchMsg = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnMsg));
				EventCenter.registerEventWatcher(EventConst.ON_UNREAD_SNSMSG_CNTCHANGE, this.mWatchMsg);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchMsg != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_UNREAD_SNSMSG_CNTCHANGE, this.mWatchMsg);
				this.mWatchMsg = null;
			}
		}
		private void OnMsg(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			EventConst mEventID = evtArgs.mEventID;
			if (mEventID != EventConst.ON_UNREAD_SNSMSG_CNTCHANGE)
			{
				return;
			}
			SnsNewMsgHintCtrl.Refresh();
		}
		private void Button_Click(object sender, RoutedEventArgs e)
		{
			BasePage rootPage = ToolFunction.GetRootPage();
			if (rootPage is CircleMainPage)
			{
				CircleMainPage circleMainPage = rootPage as CircleMainPage;
				circleMainPage.ClearTransition();
			}
			object[] object_ = null;
			int snsMsgUnreadCount = SnsMsgMgr.getSnsMsgUnreadCount();
			if (snsMsgUnreadCount > 0)
			{
				object_ = new object[]
				{
					snsMsgUnreadCount
				};
			}
			ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Common/UI/SnsMsg/SnsMessageListPage.xaml", object_, false);
		}
		public static void Refresh()
		{
			if (SnsNewMsgHintCtrl.sInstance == null)
			{
				return;
			}
			int num = SnsMsgMgr.getSnsMsgUnreadCount();
			SnsMsg last = StorageMgr.snsMsg.getLast();
			if (last == null)
			{
				SnsNewMsgHintCtrl.sInstance.DisPlay("", 0);
				return;
			}
			if (num > 99)
			{
				num = 99;
			}
			SnsNewMsgHintCtrl.sInstance.DisPlay(last.strFromUsername, num);
		}
		public void DisPlay(string strUserName, int nMsgCnt)
		{
			if (nMsgCnt <= 0)
			{
				base.set_Visibility(1);
				return;
			}
			base.set_Visibility(0);
			this.txtMsgCnt.set_Text(string.Format(strings.Album_MessageTipFormat, nMsgCnt));
			this.ctrlImgHead.ItemObject = strUserName;
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/SnsNewMsgHintCtrl.xaml", 2));
			this.ctrlImgHead = (UserHeadCtrl)base.FindName("ctrlImgHead");
			this.txtMsgCnt = (TextBlock)base.FindName("txtMsgCnt");
		}
	}
}
